<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['smart:subsidiary:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="端口名称" align="center" prop="portName"/>
      <el-table-column label="端口状态" align="center" prop="portState">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.portState == '1'">空闲</el-tag>
          <el-tag type="info" v-if="scope.row.portState == '2'">使用</el-tag>
          <el-tag type="info" v-if="scope.row.portState == '3'">维保</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['smart:subsidiary:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['smart:subsidiary:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改设备附属对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备" prop="nickName">
          <el-input v-model="deviceNickName" disabled/>
        </el-form-item>
        <el-form-item label="端口名称" prop="portName">
          <el-input v-model="form.portName" placeholder="请输入端口名称"/>
        </el-form-item>
        <el-form-item label="端口状态" prop="portState">
          <el-radio v-model="form.portState" label="1" style="width: 50px">空闲</el-radio>
          <el-radio v-model="form.portState" label="2" style="width: 50px">使用</el-radio>
          <el-radio v-model="form.portState" label="3" style="width: 50px">维保</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addSubsidiaryPort,
    updateSubsidiaryPort,
    getSubsidiaryList,
    delSubsidiaryPort,
    getSubsidiaryPort
  } from "@/api/smart/subsidiary/subsidiary";

  export default {
    name: "tableCompoment",
    props: {
      tableList: {
        type: Array,
        default: () => {
          return [];
        },
      },
      deviceNickName: {
        type: String,
        default: () => {
          return '';
        },
      },
      deviceId: {
        type: String,
        default: () => {
          return '';
        },
      },
    },
    data() {
      return {
        open: false,
        loading: false,
        // 显示搜索条件
        showSearch: true,
        form: {
          portState: '1'
        },
        rules: {
          portName: [
            {required: true, message: "请输入端口名称", trigger: "blur"}
          ],
          portState: [
            {required: true, message: "请选择端口状态", trigger: "change"}
          ],
        },
      }
    },
    methods: {
      getList() {
        getSubsidiaryList(this.deviceId).then(response => {
          this.tableList = response.data.data
          this.portOpen = true;
        })
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加端口";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSubsidiaryPort(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改端口";
        }); 
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSubsidiaryPort(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSubsidiaryPort(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除端口编号为"' + ids + '"的数据项？').then(function () {
          return delSubsidiaryPort(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      reset() {
        this.form = {
          id: null,
          deviceId: this.deviceId,
          nickName: null,
          portName: null,
          portState: '1',
          createTime: null,
          createName: null,
          updateTime: null,
          updateName: null,
        };
        this.resetForm("form");
      },

    }
  }
</script>

<style lang="scss" scoped>
  .box {
    // 必须有高度 overflow 为自动
    overflow: auto;
    height: 600px;
    width: 500px;
    padding: 0px 30px 11px 27px;

    // 滚动条的样式,宽高分别对应横竖滚动条的尺寸
    &::-webkit-scrollbar {
      width: 3px;
    }

    // 滚动条里面默认的小方块,自定义样式
    &::-webkit-scrollbar-thumb {
      background: #BABABA;
      border-radius: 2px;
    }

    // 滚动条里面的轨道
    &::-webkit-scrollbar-track {
      background: transparent;
    }
  }
</style>
